The queryStatistics
method retrieves aggregated statistics for a specific health quantity type over a defined date range. It can compute metrics such as total sum, average, minimum, maximum, most recent value, and duration, with optional support for breaking down results by source (e.g., device or app).
This method is ideal for producing daily, weekly, or historical health summaries.
quantityType: HealthQuantityType
(required)The health quantity type to query, such as:
"stepCount"
"heartRate"
"bodyMass"
"activeEnergyBurned"
HealthQuantityType
options
(optional)An object specifying filtering and configuration options for the query.
Option | Type | Description |
---|---|---|
startDate |
Date |
The start date for the query range. |
endDate |
Date |
The end date for the query range. |
strictStartDate |
boolean |
If true , only includes statistics that begin exactly at startDate . Optional. |
strictEndDate |
boolean |
If true , only includes statistics that end exactly at endDate . Optional. |
statisticsOptions |
HealthStatisticsOptions or HealthStatisticsOptions[] |
An option or array of options to define what kind of statistics to retrieve. See below. |
HealthStatisticsOptions
Option | Description |
---|---|
"cumulativeSum" |
Includes the total sum of all quantity values. |
"discreteAverage" |
Includes the average of discrete samples. |
"discreteMin" |
Includes the minimum value. |
"discreteMax" |
Includes the maximum value. |
"mostRecent" |
Includes the most recently recorded sample. |
"duration" |
Includes the total duration of all samples. |
"separateBySource" |
Separates results by source (e.g., different apps/devices). |
Returns a Promise
that resolves to a HealthStatistics
object, or null
if no data is available for the given type and range.
Use the returned HealthStatistics
object to access computed values like:
sumQuantity(...)
averageQuantity(...)
mostRecentQuantity(...)
duration(...)
statisticsOptions
is not specified, some fields (like sum, average, or most recent) may return null
.queryQuantitySamples()
instead.discreteAverage
, while step count supports cumulativeSum
.